<?php

 class __module_content_api {
 
    /**
     * Return array of contents
     *
     * int $id
     * string $uri_title
     * int $parent_id
     * int/bool $limit - if false, limit do not exist
     * int $only_cats
     *   - 1: Only cathegorys
     *   - 2: Only contents
     *   - 0: all
     * bool $id_in_key - if false {$return[] = $row;} if true {$return[$row['id']] = $row;}
     **/    
    function get($id = "", $uri_title = "", $parent_id = "", $limit = "", $only_cats = false, $id_in_key = false, $select = "a.*, b.title, b.text, b.lang, b.title_big") {
        if($id !== "")        { $where[] = "a.`id` = ?"; $array[] = $id; }
        if($uri_title !== "") { $where[] = "a.`uri_title` = BINARY(?)"; $array[] = $uri_title;}
        if($parent_id !== "") { $where[] = "a.`parent_id` = ?"; $array[] = $parent_id;}

            if($limit === false) $limit = '';
        elseif($limit ==  "")    $limit = ' LIMIT 1';
        else                     $limit = ' LIMIT '.(int)$limit;
        
            if($only_cats == 1) $limit = " AND a.`cathegory` = 1".$limit;
        elseif($only_cats == 2) $limit = " AND a.`cathegory` = 0".$limit;
        
        $res = __driver_db::si()->query('SELECT '.$select.' FROM `content` a LEFT JOIN `content_lang` b ON a.id=b.cid AND b.lang = "'.DEFAULT_LANGUAGE.'" WHERE ('.implode(" OR ", $where).')'.$limit, $array);
        while($row = &$res->fetchRow(DB_FETCHMODE_ASSOC)) {
            if($id_in_key == true) {
                $return[$row['id']] = $row;
            } else {
                $return[] = $row;
            }
        		
        }
        
        return $return;
    }
    
    function getChild($pid, $id_in_key = false, $select='a.id, b.title, a.uri_title, a.parent_id, a.cathegory') {
        $res = __driver_db::si()->query('SELECT '.$select.' FROM `content` a LEFT JOIN `content_lang` b ON a.id = b.cid AND b.`lang` = "'.DEFAULT_LANGUAGE.'" WHERE a.`parent_id` = ? ', array((int)$pid));
        
        while($row = &$res->fetchRow(DB_FETCHMODE_ASSOC)) {
            if($id_in_key == true) {
                $return[$row['id']] = $row;
            } else {
                $return[] = $row;
            }
        }
        
        return $return;
    }
    
    function getParents($id) {
        function getParent($cid /*ChildID*/) {
            $res = __driver_db::si()->query('SELECT a.id, a.parent_id, a.uid, b.title, a.uri_title, a.cathegory FROM `content` a LEFT JOIN `content_lang` b ON a.id=b.cid AND b.lang = "'.DEFAULT_LANGUAGE.'" WHERE a.`id` = ? ', array((int)$cid));
            return $res->fetchRow(DB_FETCHMODE_ASSOC);
        }
        
        
        $row = getParent($id);
        if($row['parent_id'] == 0) $while = false;
        else {
            $while = true;
            $cid = $row['parent_id'];
        }
        
        while($while) {
            $row = getParent($cid);
            if($row['parent_id'] == 0) $while=false; else $cid = $row['parent_id'];
            
            $return[] = $row;
        }

        return $return;
    }
    
    /**
     * Return int
     **/
    function getFromUri($uri, $select="*") {
        $res = __driver_db::si()->query('SELECT '.$select.' FROM `content` WHERE uri_title = "'.$uri.'" LIMIT 1', array());
        $row = $res->fetchRow(DB_FETCHMODE_ASSOC);

        return $row;
    }
 
 }
 
?>
